﻿using System;

abstract class Shape
{
    public abstract double Area { get; }
    public abstract bool IsValid();
}

class Rectangle : Shape
{
    public double Width { get; set; }
    public double Height { get; set; }

    public Rectangle(double width, double height)
    {
        Width = width;
        Height = height;
    }

    public override double Area => IsValid() ? Width * Height : 0;

    public override bool IsValid()
    {
        return Width > 0 && Height > 0;
    }
}

class Square : Rectangle
{
    public Square(double side) : base(side, side) { }

    public override bool IsValid()
    {
        return Width > 0;
    }
}

class Triangle : Shape
{
    public double A { get; set; }
    public double B { get; set; }
    public double C { get; set; }

    public Triangle(double a, double b, double c)
    {
        A = a;
        B = b;
        C = c;
    }

    public override double Area
    {
        get
        {
            if (!IsValid()) return 0;
            double s = (A + B + C) / 2;
            return Math.Sqrt(s * (s - A) * (s - B) * (s - C)); // 海伦公式
        }
    }

    public override bool IsValid()
    {
        return A > 0 && B > 0 && C > 0 && (A + B > C) && (A + C > B) && (B + C > A);
    }
}

class Program
{
    static void Main()
    {

        //test
        Shape rect = new Rectangle(5, 10);
        Console.WriteLine($"矩形：合法={rect.IsValid()}, 面积={rect.Area}");

        Shape square = new Square(4);
        Console.WriteLine($"正方形：合法={square.IsValid()}, 面积={square.Area}");

        Shape triangle = new Triangle(3, 4, 5);
        Console.WriteLine($"三角形：合法={triangle.IsValid()}, 面积={triangle.Area}");

        Shape Triangle2 = new Triangle(1, 2, 10);
        Console.WriteLine($"非法三角形：合法={Triangle2.IsValid()}, 面积={Triangle2.Area}");
    }
}

